import { ref } from 'vue'
import { defineStore } from 'pinia'
import Cookies from 'js-cookie'
import localforage from 'localforage'
// import router from '@/router'
import _ from 'lodash'

// 使用cookies作为存储方式
const cookiesStorage = {
 setItem(key, state) {
  return Cookies.set(key, state)
 },
 getItem(key) {
  return Cookies.get(key)
 },
}
// 使用localforage作为存储方式
const localforageStorage = {
 setItem(key, state) {
  return localforage.setItem(key, state)
 },
 getItem(key) {
  return localforage.getItem(key)
 },
}
export const useStore = defineStore(
 'store',
 () => {
  //------------------------------------------------------------
  const rootWidth = ref(0);
  const rootHeight = ref(0);
  //------------------------------------------------------------
  // 声明需要缓存的数据
  const userInfo = ref({}) // 用户信息
  // 重写
  const $reset = () => {
   userInfo.value = {}
   rootWidth.value = 0
   rootHeight.value = 0
  }
  // 需要暴露给外部调用的对象和方法
  return {
   userInfo,
   rootWidth,
   rootHeight,
   $reset,
  }
 },
 {
  persist: [
   {
    // 自定义存储的 key，默认是 store.$id
    key: 'local',
    // 可以指定任何 Storage 的实例，默认是 localStorage
    storage: localStorage,
    // 部分持久化状态的点符号路径数组，[]意味着没有状态被持久化(默认为undefined，持久化整个状态)
    pick: [
     'userInfo',
     'rootWidth',
     'rootHeight',
    ],
   },
   {
    key: 'session',
    storage: sessionStorage,
    pick: ['hasAddrRoute', 'allTabs', 'cacheRouteList'],
   },
   {
    key: 'cookie',
    storage: cookiesStorage,
    pick: ['rememberAccount', 'kyLoginId', 'token'],
   },
   {
    key: 'indexDB',
    storage: localforageStorage,
    pick: [],
   },
  ],
 },
)